package com.samsung.service.impl.timer;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.samsung.dao.timer.TimerEventMapper;
import com.samsung.dao.timer.TimerRecordMapper;
import com.samsung.dto.timer.StopwatchDto;
import com.samsung.model.timer.TimerEvent;
import com.samsung.model.timer.TimerRecord;
import com.samsung.service.timer.StopwatchService;

@Service("stopwatchService")
public class StopwatchServiceImpl implements StopwatchService {

	@Resource
	private TimerEventMapper timerEventMapper;

	@Resource
	private TimerRecordMapper timerRecordMapper;

	@Override
	public StopwatchDto getActivity(String tid) {
		TimerEvent event = timerEventMapper.selectByPrimaryKey(tid);
		List<TimerRecord> recordList = timerRecordMapper.selectByTimerId(tid);
		
		if(event==null || recordList==null) {
			return null;
		}
		
		StopwatchDto dto = new StopwatchDto();
		dto.setTitle(event.getTitle());
		dto.setType(event.getType());
		dto.setRecordList(recordList);
		
		return dto;
	}
	
	@Override
	public StopwatchDto getMyActivity(String uid) {
		TimerEvent event = timerEventMapper.selectByUid(uid);
		List<TimerRecord> recordList = timerRecordMapper.selectByTimerUid(uid);
		
		if(event==null || recordList==null) {
			return null;
		}
		
		StopwatchDto dto = new StopwatchDto();
		dto.setTitle(event.getTitle());
		dto.setType(event.getType());
		dto.setRecordList(recordList);
		
		return dto;
	}
	
	@Override
	public void saveRecord(TimerRecord record) {
		timerRecordMapper.insert(record);
	}
	
	@Override
	public void saveEvent(TimerEvent event) {
		timerEventMapper.insert(event);
	}

}
